/*
 * @Author: your name
 * @Date: 2024-04-18 11:18:53
 * @LastEditTime: 2024-04-18 11:25:50
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\515.在每个树行中找最大值.cpp
 */
/*
 * @lc app=leetcode.cn id=515 lang=cpp
 *
 * [515] 在每个树行中找最大值
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        vector<int>res;
        queue<TreeNode*> que;
        if(root == nullptr) return res;
        que.push(root);
        while(!que.empty()){
            int min_val = INT_MIN;
            int size = que.size();
            for(int i = 0; i < size; i++){
                TreeNode* tmp = que.front();
                que.pop();
                min_val = max(min_val, tmp->val);
                if(tmp->left) que.push(tmp->left);
                if(tmp->right) que.push(tmp->right);
            }
            res.push_back(min_val);
        }
        return res;
    }
};
// @lc code=end

